/*
 * PKL 51 GUNUNG KIDUL
 * Subseksie DATA ENTRY + CAPI
 */
package pkl51.service.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import pkl51.database.DatabaseUtility;
import pkl51.entity.Blok6b;
import pkl51.error.EntityException;
import pkl51.service.Blok6bDao;

/**
 *
 * @author toshiba
 */
public class Blok6bDaoImpl implements Blok6bDao{
    
    private Connection connection;
    private final String insertBlok6b = "INSERT INTO blok6b VALUES (?, ?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE B6BC1 = ?, B6BC2 = ?, B6BC3 = ?, B6BC4 = ?, B6BC5 = ?";
    private final String updateBlok6b = "UPDATE blok6b SET B6BC1 = ?, B6BC2 = ?, B6BC3 = ?, B6BC4 = ?, B6BC5 = ? WHERE NKS = ? AND ID_PANGAN = ?";
    private final String getBlok6bByNKS_IdPangan = "SELECT * FROM blok6b WHERE NKS = ? AND ID_PANGAN = ?";

    @Override
    public void insertBlok6b(Blok6b blok6b) throws EntityException {
        PreparedStatement statement = null;
        try {
            connection = DatabaseUtility.getConnection();
            connection.setAutoCommit(false);
            statement = connection.prepareStatement(insertBlok6b);

            statement.setString(1, blok6b.getNKS());
            statement.setString(2, blok6b.getID_PANGAN());
            statement.setDouble(3, blok6b.getB6BC1());
            statement.setInt(4, blok6b.getB6BC2());
            statement.setDouble(5, blok6b.getB6BC3());
            statement.setInt(6, blok6b.getB6BC4());
            statement.setDouble(7, blok6b.getB6BC5());
            statement.setDouble(8, blok6b.getB6BC1());
            statement.setInt(9, blok6b.getB6BC2());
            statement.setDouble(10, blok6b.getB6BC3());
            statement.setInt(11, blok6b.getB6BC4());
            statement.setDouble(12, blok6b.getB6BC5());
            statement.executeUpdate();

            connection.commit();

        } catch (SQLException ex) {
            try {
                connection.rollback();
            } catch (SQLException ex1) {
                Logger.getLogger(Blok6bDaoImpl.class.getName()).log(Level.SEVERE, null, ex1);
            }
            Logger.getLogger(Blok6bDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new EntityException(ex.getMessage());
            
        } finally {
            try {
                connection.setAutoCommit(true);
            } catch (SQLException ex) {
                Logger.getLogger(Blok6bDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(Blok6bDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    @Override
    public void updateBlok6b(Blok6b blok6b) throws EntityException {
        PreparedStatement statement = null;
        try {
            connection = DatabaseUtility.getConnection();
            connection.setAutoCommit(false);
            statement = connection.prepareStatement(updateBlok6b); //isikan statement yang akan dieksekusi

            statement.setDouble(1, blok6b.getB6BC1());
            statement.setInt(2, blok6b.getB6BC2());
            statement.setDouble(3, blok6b.getB6BC3());
            statement.setInt(4, blok6b.getB6BC4());
            statement.setDouble(5, blok6b.getB6BC5());
            statement.setString(6, blok6b.getNKS());
            statement.setString(7, blok6b.getID_PANGAN());
                                   
            statement.executeUpdate();
            connection.commit();
        } catch (SQLException ex) {
            try {
                connection.rollback();
            } catch (SQLException ex1) {
                Logger.getLogger(Blok6bDaoImpl.class.getName()).log(Level.SEVERE, null, ex1);
            }
            Logger.getLogger(Blok6bDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                connection.setAutoCommit(true);
            } catch (SQLException ex) {
                Logger.getLogger(Blok6bDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(Blok6bDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    @Override
    public Blok6b getBlok6bByNKS_IdPangan(String NKS, String ID_PANGAN) throws EntityException {
        PreparedStatement statement = null;
        try {
            connection = DatabaseUtility.getConnection();
            connection.setAutoCommit(false);
            statement = connection.prepareStatement(getBlok6bByNKS_IdPangan); //isikan statement yang akan dieksekusi

            statement.setString(1, NKS);
            statement.setString(2, ID_PANGAN);
             ResultSet resultSet = statement.executeQuery();

            Blok6b blok6b = null;
            if (resultSet.next()) {
                blok6b = new Blok6b();
                blok6b.setNKS(resultSet.getString("NKS"));
                blok6b.setID_PANGAN(resultSet.getString("ID_PANGAN"));
                blok6b.setB6BC1(resultSet.getDouble("B6BC1"));
                blok6b.setB6BC2(resultSet.getInt("B6BC2"));
                blok6b.setB6BC3(resultSet.getDouble("B6BC3"));
                blok6b.setB6BC4(resultSet.getInt("B6BC4"));
                blok6b.setB6BC5(resultSet.getDouble("B6BC5"));              
            }

            connection.commit();

            return blok6b;
            
        } catch (SQLException ex) {
            try {
                connection.rollback();
            } catch (SQLException ex1) {
                Logger.getLogger(Blok6bDaoImpl.class.getName()).log(Level.SEVERE, null, ex1);
            }
            Logger.getLogger(Blok6bDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new EntityException(ex.getMessage());
        } finally {
            try {
                connection.setAutoCommit(true);
            } catch (SQLException ex) {
                Logger.getLogger(Blok6bDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                    Logger.getLogger(Blok6bDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    
    
    
}
